package com.odm.communitymanage.ui.mine.MySeekHelp

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.AllSeekHelp
import com.odm.communitymanage.core.bean.SeekHelpData
import com.odm.communitymanage.core.net.ApiService
import com.rxjava.rxlife.lifeOnMain
import com.zhengsr.tablib.view.adapter.TabFlowAdapter
import com.zhengsr.tablib.view.flow.TabFlowLayout
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_my_seek_help.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 我的 求助页面
 * @author: ODM
 * @date: 2020/4/15
 */
class MySeekHelpFragment : BaseMVVMFragment(){

    private var rvAdapter: MySeekHelpAdapter ?= null


    private var disposable : Disposable?= null

    override fun onDestroyView() {
        super.onDestroyView()
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }


    override fun initViewModelObserve() {

    }

    override fun initViews() {
        initTabLayout()

        ivBack_MySeekHelp.setOnClickListener {
            pop()
        }
        
        // 初始化 recyclerView 和 适配器
        rvMySeekHelps.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = MySeekHelpAdapter(mutableListOf())
        rvMySeekHelps.adapter = rvAdapter
        rvAdapter?.animationEnable = true
        //设置空布局
        rvAdapter?.setEmptyView(R.layout.layout_empty_list)
        rvAdapter?.setOnItemClickListener{ adapter, view, position ->
            LiveEventBus
                .get(Constants.EVENT_MY_HELP_DETAIL)
                .post(rvAdapter?.data?.get(position))
            start(MySeekHelpDetailFragment())

        }
        //默认加载未解决列表
        getMyUnSolvedHelpList()
    }

    /**
     * 获取我的已经解决求助的列表
     */
    private fun getMySolvedHelpList(){
        disposable  = RxHttp.get(ApiService.MY_SOLVED_SEEK_HELP)
            .asClass(AllSeekHelp::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(it.code == 1){
                    // 列表数据为空，显示空状态布局
                    // 列表数据不为空显示内容布局，使用setList
                    val dataList : MutableList<SeekHelpData> = it.data.reversed().toMutableList()
                    if(dataList.isEmpty()){
                        msv_my_seek_help.showEmpty()
                    }else {
                        msv_my_seek_help.showContent()
                        rvAdapter?.setList(dataList)
                    }
                }else {
                    ToastUtils.showShort(it.message)
                    msv_my_seek_help.showEmpty()
                }
            } , {
                ToastUtils.showShort(it.message)
                msv_my_seek_help.showError()
            })
    }

    /**
     * 获取我的未解决求助的列表
     */
    private fun getMyUnSolvedHelpList(){
        disposable  = RxHttp.get(ApiService.MY_UNSOLVED_SEEK_HELP)
            .asClass(AllSeekHelp::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(it.code == 1){
                    // 列表数据为空，显示空状态布局
                    // 列表数据不为空显示内容布局，使用setList
                    val dataList : MutableList<SeekHelpData> = it.data.reversed().toMutableList()
                    if(dataList.isEmpty()){
                        msv_my_seek_help.showEmpty()
                    }else {
                        msv_my_seek_help.showContent()
                        rvAdapter?.setList(dataList)
                    }
                }else {
                    ToastUtils.showShort(it.message)
                    msv_my_seek_help.showEmpty()
                }
            } , {
                ToastUtils.showShort(it.message)
                msv_my_seek_help.showError()
            })
    }





        /**
         * 初始化 TabLayout
         */
        private fun initTabLayout(){
            //TabLayout点击事件
            tabMySeekHelp?.configTabLayoutConfig {
                //选中项的view的回调
                onSelectViewChange = { fromView, selectViewList, reselect ->
/*                    val toView = selectViewList.first()
                    if (reselect) {
                        //重复选择
                    }*/
                }
                //选中项index的回调事件--显示不同排序的瀑布流List
                onSelectIndexChange = { fromIndex, selectIndexList, reselect ->
                    //toIndex 为 选中 Index
                    val toIndex = selectIndexList.first()
                    when(toIndex){
                        0 -> {
                            msv_my_seek_help.showLoading()
                            getMyUnSolvedHelpList()
                        }
                        1 -> {
                            msv_my_seek_help.showLoading()
                            getMySolvedHelpList()
                        }
                        else -> {}
                    }
                }
            }
        }



    override fun getLayoutId(): Int {
        return R.layout.fragment_my_seek_help
    }
}